package com.shutuo.callnummeiwei;


import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import android.text.TextUtils;
import android.util.Log;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.instwall.callnumlistener.CallNumNotification;
import com.shutuo.callnummeiwei.core.C;
import com.shutuo.callnummeiwei.core.SocketManager;
import com.shutuo.callnummeiwei.global.GlobalConstant;
import com.shutuo.callnummeiwei.global.NetLib;
import com.shutuo.callnummeiwei.socket.SocketCommunicateManager;
import com.shutuo.callnummeiwei.utils.MeiWeiJsParamsUtils;

import org.json.JSONException;
import org.json.JSONObject;
import ashy.earl.common.app.App;
import ashy.earl.common.util.L;


public class CommonCallnumber  {
    public static CommonCallnumber instance;
    private SocketManager socketManager = null;
    private LocalBroadcastManager mLocalBroadcastManager;
    private SocketCommunicateManager socketCommunicateManager;
    private int RECEIVER_TYPE = 0;
    private int MEIWEI_TYPE = 1;


    private CommonCallnumber() {
        try {
            socketManager = SocketManager.getInstance();
            socketManager.init((Application) App.getAppContext());
            NetLib.getInstance().init(App.getAppContext());
        } catch (Exception e){
            e.printStackTrace();
        }
        mLocalBroadcastManager = LocalBroadcastManager.getInstance(App.getAppContext());
    }


    public static CommonCallnumber getInstance(){
        if (instance == null) {
            synchronized (CommonCallnumber.class){
                if (instance == null){
                    instance = new CommonCallnumber();
                }
            }
        }
        return instance;
    }

    public void init(){
        L.d(GlobalConstant.TAG, "%s", "[CommonCallnumber] [init]");
        registerReceiver();

        socketManager.initServer().startServer(3526);
        socketCommunicateManager = SocketCommunicateManager.getInstance();
        startGetMeiweiShopInfoTask();

    }

    private void registerReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(C.SERVER_STATE_ACTION);
        filter.addAction(C.SERVER_CLIENT_STATE_ACTION);
        filter.addAction(C.SERVER_NEW_CLIENT_MSG);
        mLocalBroadcastManager.registerReceiver(mReceiver, filter);
    }



    /**
     * 启动美味不用等任务
     */
    private void startGetMeiweiShopInfoTask() {
        L.d(GlobalConstant.TAG, "%s", "[CommonCallnumber] startGetMeiweiShopInfoTask");

        socketCommunicateManager.startSendMsg();
        socketCommunicateManager.setMeiWeiDataChangeListner(new SocketCommunicateManager.MeiWeiDataChangeListner() {
            @Override
            public void meiWeiDataChange(JSONObject str) {
                L.d(GlobalConstant.TAG, "%s", "[CommonCallnumber] [startGetMeiweiShopInfoTask] meiWeiDataChange:" + str.toString());
                String jsParams = MeiWeiJsParamsUtils.constructToJsParams(str,MEIWEI_TYPE,false);
            }
        });
    }


    BroadcastReceiver mReceiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent){
            String jsParams = null;
            if (intent.getAction().equals(C.SERVER_STATE_ACTION)) {
                boolean isReady = intent.getBooleanExtra("data", false);
            } else if (intent.getAction().equals(C.SERVER_NEW_CLIENT_MSG)) {
                String msg = intent.getStringExtra("msg");
                int type = intent.getIntExtra("type", -1);

                L.d(GlobalConstant.TAG, "%s", "[CommonCallnumber] [BroadcastReceiver] onReceive msg:" + msg);
                if (TextUtils.isEmpty(msg)){
                    return;
                }
                if (msg.startsWith("MsgHello")){
                    Log.d("lk", "[CommonCallnumber] [BroadcastReceiver] 客户端版本消息");
                    return;
                }
                try {
                    JSONObject jsonObject = new JSONObject(msg);

                    if (type == C.TYPE_CALLNUMBER) {
                        jsParams = MeiWeiJsParamsUtils.constructToJsParams(jsonObject,RECEIVER_TYPE,false);
                    } else if (type == C.TYPE_CALLNUMBER_QUEUE) {
                        jsParams = MeiWeiJsParamsUtils.constructToJsParams(jsonObject,RECEIVER_TYPE,true);
                    }
                    //send data
                    if (!TextUtils.isEmpty(jsParams)){
                        CallNumNotification.getCallNumNotification().notificationClient(jsParams);
                    }
                    } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else if (intent.getAction().equals(C.SERVER_CLIENT_STATE_ACTION)) {
                String clientInfo = intent.getStringExtra("clientInfo");
            }
        }
    };



    /**
     * 释放资源
     */
    public void release() {
        instance = null;
        mLocalBroadcastManager.unregisterReceiver(mReceiver);
    }


}